/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.util;

import java.util.ListIterator;
import java.util.List;

/**
 *
 * @author Yuqing Tang
 */
public class MyListIterator<E> implements ListIterator<E> {

    private List<E> mList;
    private int mIndex = 0;
    private int hashCode = 0;

    @Override
    public int hashCode() {
        if (0 == hashCode) {
            hashCode = 17;
            hashCode = 37 * hashCode + mIndex;
            hashCode = 37 * hashCode + mList.hashCode();
        }
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final MyListIterator rhs = (MyListIterator) obj;
        boolean res = (mIndex == rhs.mIndex);
        if (mList != null) {
            res = res && mList.equals(rhs.mList);
        } else {
            res = res && rhs.mList == null;
        }

        return res;
    }

    public MyListIterator(List<E> list) {
        mList = list;
        mIndex = 0;
    }

    public MyListIterator(List<E> list, int i) {
        mList = list;
        mIndex = i;
    }

    public MyListIterator(MyListIterator<E> orig) {
        mList = orig.mList;
        mIndex = orig.mIndex;
    }

    public E get() {
        return mList.get(mIndex);
    }

    public void add(E element) {
        mList.add(mIndex, element);
    }

    public void set(E element) {
        mList.set(mIndex, element);
    }

    ///TODO: need to check
    public void remove() {
        mList.remove(mIndex);

    }

    public int previousIndex() {
        if (mIndex > -1) {
            return mIndex;
        } else {
            return -1;
        }
    }

    public MyListIterator previousIterator() {
        MyListIterator res = new MyListIterator(this);
        res.previous();
        return res;
    }

    public MyListIterator nextIterator() {
        MyListIterator res = new MyListIterator(this);
        res.next();
        return res;
    }

    public int nextIndex() {
        if (mIndex < mList.size()) {
            return mIndex;
        } else {
            return mList.size();
        }
    }

    public boolean hasPrevious() {
        return (mIndex > 0);
    }

    public E previous() throws java.lang.IndexOutOfBoundsException {
        if (!hasPrevious()) {
            throw new java.lang.IndexOutOfBoundsException();
        }
        --mIndex;
        E res = mList.get(mIndex);
        return res;
    }

    public boolean hasNext() {
        return (mIndex < mList.size());
    }

    public E next() throws java.lang.IndexOutOfBoundsException {
        if (!hasNext()) {
            throw new java.lang.IndexOutOfBoundsException();
        }
        E res = mList.get(mIndex);
        ++mIndex;
        return res;
    }

    public void reset() {
        mIndex = 0;
    }
}
